package yushuman.rbac.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 异常统一处理工具类
 *
 * @author 俞舒熳
 * @since 2025/6/7 00:18
 */
public class ExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

    private ExceptionHandler() {

    }

    private static void printError(String message) {
        System.out.println("\033[31m" + message + "\033[0m");
    }

    public static void handle(Exception e) {
        if(e instanceof FatalException) {
            logger.error("致命错误：{}",e.getMessage(), e);
            printError(e.getMessage());
            System.exit(1);
            return;
        }
        if (e instanceof PersistenceException) {
            logger.error("数据持久化异常：{}", e.getMessage(), e);
            printError(e.getMessage());
            return;
        }
        if (e instanceof BaseRuntimeException) {
            logger.error("运行异常：{}", e.getMessage(), e);
            printError(e.getMessage());
            return;
        }
        logger.error("未知异常：{}", e.getMessage(), e);
        printError(e.getMessage());
    }
}
